﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Phoenix.Backend
{
    public partial class Users : System.Web.UI.Page
    {
        static string idpermission;
        protected DataTable dt;
        DataTable dtUsers;
        string success_create = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'/>" + "Cập nhật thông tin thành công. Tên tài khoản và mật khẩu giống nhau." + "</p></div>";
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'/>" + "Cập nhật thông tin thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'/>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        string info = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'/>" + "Thông tin chưa được cập nhật." + "</p></div><br  class='clear'/>";
        Phoenix.AppCode.Entities.Users users = new Phoenix.AppCode.Entities.Users();
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Request.QueryString["Action"] == "Create" || Request.QueryString["Action"] == "Edit" || Request.QueryString["Action"] == "ResetPwd" || Request.QueryString["Action"] == "Permission" || Request.QueryString["Action"] == "AddPermission" || Request.QueryString["Action"] == "Delete")
                    {
                        add.Visible = false;
                        edit.Visible = false;
                        Load_ActionUsers(Request.QueryString["Action"]);
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("Error.aspx");
                }
            }
        }

        #region  Load action Users
        private void Load_ActionUsers(string action)
        {
            try
            {
                //DirectoryInfo dirLanguage = new DirectoryInfo(Server.MapPath(@"~\" + @"Languages"));
                if (action == "Create")
                {
                    add.Visible = true;
                    edit.Visible = false;
                    list.Visible = false;

                }
                else if (action == "Edit")
                {
                    add.Visible = false;
                    edit.Visible = true;
                    list.Visible = false;

                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.UsersService.UsersList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                    txtUserNameE.Text = dt_Edit.Rows[0]["UserName"].ToString();
                    txtFullNameE.Text = dt_Edit.Rows[0]["FullName"].ToString();
                    txtPositionE.Text = dt_Edit.Rows[0]["Position"].ToString();
                    txtEmailE.Text = dt_Edit.Rows[0]["Email"].ToString();
                    txtPhoneE.Text = dt_Edit.Rows[0]["Phone"].ToString();
                    txtCreateDate.Text = dt_Edit.Rows[0]["CreateDate"].ToString();
                    txtLastLogin.Text = dt_Edit.Rows[0]["LastLogin"].ToString();
                    if (dt_Edit.Rows[0]["Visible"].ToString().Equals("True")) { chbActiveE.Checked = true; } else { chbActiveE.Checked = false; }
                }
                else if (action == "ResetPwd")
                {
                    try
                    {
                        users = Phoenix.AppCode.Bussiness.UsersService.GetUsersByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.UsersService.Delete(users);
                        lblWarning.Text = success;
                        // Response.Redirect("Users.aspx");
                    }
                    catch
                    {
                        Response.Redirect("Error.aspx");
                    }
                }
                else if (action == "Delete")
                {
                    try
                    {
                        users = Phoenix.AppCode.Bussiness.UsersService.GetUsersByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.UsersService.Delete(users);
                        lblWarning.Text = success;
                        // Response.Redirect("Users.aspx");
                    }
                    catch
                    {
                        Response.Redirect("Error.aspx");
                    }
                }
                else
                {
                    add.Visible = false;
                    edit.Visible = false;
                    list.Visible = true;
                }
            }
            catch
            {
            }
        }
        #endregion

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                users.ID_Users = DateTime.Now.ToString("yyyyMMdd");
                users.UserName = txtUserNameA.Text;
                users.Pass = txtUserNameA.Text;
                users.FullName = txtFullNameA.Text;
                users.Position = txtPositionA.Text;
                users.Email = txtEmailA.Text;
                users.Phone = txtPhoneA.Text;
                users.Visible = chbActiveA.Checked;
                
                Phoenix.AppCode.Bussiness.UsersService.Create(users);

                ClearAllFields();
                lblWarningA.Text = success_create;
                //Response.Redirect("Users.aspx?Action=Create");
            }
            catch
            {
                lblWarningA.Text = error;
                //Response.Redirect("Error.aspx");
            }
        }
        protected void btnCancelA_Click(object sender, EventArgs e)
        {
            ClearAllFields();
            lblWarningA.Text = info;
        }

        private void ClearAllFields()
        {
            txtUserNameA.Text = "";
            txtFullNameA.Text = "";
            txtPositionA.Text = "";
            txtEmailA.Text = "";
            txtPhoneA.Text = "";
            chbActiveA.Checked = false;
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            users = Phoenix.AppCode.Bussiness.UsersService.GetUsersByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            users.FullName = txtFullNameE.Text;
            users.Position = txtPositionE.Text;
            users.Email = txtEmailE.Text;
            users.Phone = txtPhoneE.Text;
            users.Visible = chbActiveE.Checked;
            Phoenix.AppCode.Bussiness.UsersService.Edit(users);
            lblWarningE.Text = success;
            //Response.Redirect("Users.aspx?Action=Edit&ID=" + Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()) + "");

        }
        protected void btnCancelE_Click(object sender, EventArgs e)
        {
            DataTable dt_Edit = Phoenix.AppCode.Bussiness.UsersService.UsersList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            txtUserNameE.Text = dt_Edit.Rows[0]["UserName"].ToString();
            txtFullNameE.Text = dt_Edit.Rows[0]["FullName"].ToString();
            txtPositionE.Text = dt_Edit.Rows[0]["Position"].ToString();
            txtEmailE.Text = dt_Edit.Rows[0]["Email"].ToString();
            txtPhoneE.Text = dt_Edit.Rows[0]["Phone"].ToString();
            txtCreateDate.Text = dt_Edit.Rows[0]["CreateDate"].ToString();
            txtLastLogin.Text = dt_Edit.Rows[0]["LastLogin"].ToString();
            if (dt_Edit.Rows[0]["Visible"].ToString().Equals("True")) { chbActiveE.Checked = true; } else { chbActiveE.Checked = false; }
            lblWarningE.Text = info;
        }
    }
}